package com.cjjd.c.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjjd.c.R;


/**
 * 自定义actionbar组件
 */
public class ActionBarView extends RelativeLayout {

    private static final String TAG = "ActionBarView";

    /**
     * 标题
     */
    private TextView mTvTitle;
    /**
     * 左键
     */
    private Button mBtnLeft;
    /**
     * 右键
     */
    private Button mBtnRight;

    /**
     * 中间布局
     */
    private RelativeLayout mRlMiddle;
    // max中间布局
    private RelativeLayout mMiddleContent;


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public ActionBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mBtnLeft = (Button) findViewById(R.id.btn_left);
        mBtnRight = (Button) findViewById(R.id.btn_right);
        mTvTitle = (TextView) findViewById(R.id.tv_detail_title);
        mRlMiddle = (RelativeLayout) findViewById(R.id.rl_middle);
        mMiddleContent = (RelativeLayout) findViewById(R.id.mid_frame);

    }

    /**
     * 是否显示标题栏
     */
    public void show(boolean show) {
        setVisibility(show ? VISIBLE : GONE);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(Object title) {
        if (title == null) {
            show(false);
            return;
        }

        if (title.equals(0)) {
            show(false);
            return;
        }

        if (title instanceof Integer) {
            mTvTitle.setText((Integer) title);
        } else if (title instanceof String) {
            mTvTitle.setText((String) title);
        }

        mTvTitle.setVisibility(View.VISIBLE);

    }

    /**
     * add布局到标题栏中间
     *
     * @param v
     */
    public void setMiddleLayout(View v) {
        mTvTitle.setVisibility(GONE);
        mRlMiddle.addView(v, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        show(true);
    }

    public RelativeLayout getMiddleView() {
        return mRlMiddle;
    }

    public RelativeLayout getMiddleContent(){
        return mMiddleContent;
    }

    /**
     * 获取左键
     *
     * @return
     */
    public Button getLeftButton() {
        return mBtnLeft;
    }

    /**
     * 获取右键
     *
     * @return
     */
    public Button getRightButton() {
        return mBtnRight;
    }

    public TextView getTitleView() {
        return mTvTitle;
    }

    /**
     * 事件监听
     */
    public interface ActionBarListener {

        /**
         * 左键事件
         */
        void onLeftButtonClick();

        /**
         * 右键事件
         */
        void onRightButtonClick();

        /**
         * 设置左键
         */
        void initLeftButton(Button leftButton);

        /**
         * 设置右键
         */
        void initRightButton(Button rightButton);

        /**
         * 获取title
         */
        Object getTitleString();

        /**
         * 获得contentView
         */
        int getContentView();

    }

}